﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HANS.DataBaseTool;
using HANS_BackStage.Model.DbModel;
using HANS_BackStage.Model.WebModel;
using HANS.Utility.Extensions;

namespace HANS_BackStage.Impl
{
    public   class AdminServices
    {
       private static  HANS_BackStage.DAL.BackStageDataEntityDAL<HANS_BackStage.Model.DbModel.Backstage_admin> adminDAL = new DAL.BackStageDataEntityDAL<Model.DbModel.Backstage_admin>();


        /// <summary>
        /// 验证登录
        /// </summary>
        /// <param name="Login_admin"></param>
        /// <returns></returns>
        public static Msg validate(HANS_Login Login_admin) {
            Msg msg = new Msg();
            var admin = adminDAL.Load(Login_admin.Nane);
            if (admin!=null)
            {
                var encryPassword = Login_admin.Password.GetMD5Hash();
                if (encryPassword.Equals(admin.Password))
                {
                    msg.AddMsg("登录成功！");
                    msg.MsgObjectContent = admin as Backstage_admin;
                }
                else
                {
                    msg.Result = false;
                    msg.AddMsg("账号，密码不匹配!");
                }
            }
            else
            {
                msg.Result = false;
                msg.AddMsg("管理员不存在！");
            }
           
            return msg;
        }

        /// <summary>
        /// 根据账户获取管理员
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static  Backstage_admin GetByName(string Name) {
            return adminDAL.Load(Name);
        }


        /// <summary>
        /// 根据ID获取管理员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Backstage_admin GetById(string id) {
            try
            {
                return adminDAL.List(new Backstage_admin() { Id = id })[0];
            }
            catch (Exception)
            {

                return null; 
            }
            
        }
        /// <summary>
        /// 获取全部管理员
        /// </summary>
        /// <returns></returns>
        public static List<Backstage_admin> All() {
            return adminDAL.List();
        }

        /// <summary>
        /// 更新上次登录时间
        /// </summary>
        /// <param name="admin"></param>
        public static void UpdateLastLoginDate(Backstage_admin admin) {
            admin.Lastlogindate = DateTime.Now;
            adminDAL.Update(admin);
        }

        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public static Msg CreateAdmin(Backstage_admin admin) {
            Msg msg = new Msg();
            if (adminDAL.Insert(admin)>0)
            {
                msg.AddMsg("添加成功！");
                msg.Result = true;
            }
            else
            {
                msg.AddMsg("添加失败！");
                msg.Result = false;
            }

            return msg;

        }

        /// <summary>
        /// 修改后台管理员
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public static Msg UpdateAdmin(Backstage_admin admin) {
            Msg msg = new Msg();
            if (adminDAL.Update(admin)>0)
            {
                msg.AddMsg("修改成功！");
                msg.Result = true;
            }
            else
            {
                msg.AddMsg("修改失败！");
                msg.Result = false;
            }
            return msg;
        }


        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Msg DeleteAdmin(string id) {
            Msg msg = new Msg();
            if (adminDAL.Delete(id)>0)
            {
                msg.Result = true;
                msg.AddMsg("删除成功！");
            }
            else
            {
                msg.Result = false;
                msg.AddMsg("删除时报！");
            }
            return msg;
        }

        /// <summary>
        /// 根据角色ID获取相关的管理员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<Backstage_admin> GetAdminByRole(string id)
        {
            List<Backstage_admin> List_admin = new List<Backstage_admin>();
            List<string> adminid_List = Role_PermiServices.GetAdminStrByRoleId(id);
            foreach (var item in adminid_List)
            {
                Backstage_admin admin = GetById(item);
                if (admin!=null)
                {
                    List_admin.Add(admin);
                }
                
            }
            return List_admin;
        }

        /// <summary>
        /// 个人中心修改新密码
        /// </summary>
        /// <param name="adminEntity"></param>
        /// <param name="oldpassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public static bool SetIfChangePassword(Backstage_admin adminEntity, string oldpassword, string newPassword)
        {
            if (newPassword.IsNotNullAndNotWhiteSpace())
            {
                if (oldpassword.GetMD5Hash()!=adminEntity.Password)
                {
                    return false;
                }
                adminEntity.Password = newPassword.GetMD5Hash();
            }

            return true;
        }
    }
}
